<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/1 0001
 * Time: 下午 4:09
 */

namespace app\mobile\controller;

use app\admin\model\Cate;
use app\admin\model\CategoryModule;
use app\admin\model\CateList;
use app\admin\model\Content;
use app\admin\model\Lesson;
use app\admin\model\LessonCate;
use service\ToolsService;
use think\Db;

class Category extends MoBase {
    public function index()
    {
        $pid = $this->request->param( 'pid' , '' );
        $id  = $this->request->param( 'id' , '' );
        //一级分类
        $pcate = CategoryModule::get( $pid );
        //小分类
        $catelist = CategoryModule::where( [ 'pid' => $pid , 'status' => 1 , 'is_deleted' => 1 ] )->order( 'sort asc,id asc' )->select()->toArray();
        //
        if ( $id ) {
            $cur = CategoryModule::get( $id )->toArray();
        } elseif ( !empty( $catelist ) ) {
            $cur = $catelist[ 0 ];
        } else {
            $cur = $pcate;
        }
        $this->assign( [
            'catelist' => $catelist ,
            'cur'      => $cur ,
            'pcate'    => $pcate
        ] );
        $this->data_filter( $cur );
        return view( $cur[ 'index_template' ] ?: '' );
    }

    public function data_filter( $category = [] )
    {
        //近期培训
        if ( $category[ 'id' ] == 39 ) {
            $db = Lesson::where( [ 'type' => 39 , 'is_deleted' => 1 , 'status' => 1 ] )->field( 'id,title,cover,cover_desc' )->order( 'sort asc,id asc' )->select()->toArray();
            foreach ( $db as $k => $v ) {
                $db[ $k ][ 'cover_desc' ] = json_decode( $v[ 'cover_desc' ] , true );
            }
            $cate = $db;
        } else
            //安全保障
            if ( $category[ 'id' ] == 14 ) {
                $cate = Db::name( 'safety' )->where( [ 'pid' => 14 , 'status' => 1 , 'is_deleted' => 1 ] )->field( 'title,content' )->order( 'sort asc,id asc' )->select();
            } else
                //课程体系
                if ( $category[ 'id' ] == 12 ) {
                    $pid      = $this->request->get( 'pid' , '' );
                    $keywords = $this->request->get( 'keywords' , '' );
                    $page     = $this->request->get( 'page' , 1 );

                    $lesson_cate = LessonCate::where( [ 'pid' => 0 , 'is_deleted' => 1 , 'status' => 1 ] )->field( 'id,name' )->order( 'sort asc,id asc' )->select();
                    empty( $pid ) && $pid = $lesson_cate[ 0 ][ 'id' ];
                    $lesson       = Lesson::getbyfilter( [ 'pid' => $pid , 'keywords' => $keywords ] )->select();
                    $lesson_total = Lesson::getbyfilter( [ 'pid' => $pid , 'keywords' => $keywords ] )->count();

                    //已选分类
                    $select    = LessonCate::where( 'id' , 'in' , explode( ',' , $pid ) )->where( 'pid' , '<>' , 0 )->field( 'id,name,pid' )->select()->toArray();
                    $ppid_name = Db::name( 'lesson_cate' )->where( 'id' , explode( ',' , $pid )[ 0 ] )->value( 'name' );
                    $this->assign( [
                        'lesson_cate'   => $lesson_cate ,
                        'lesson_filter' => $this->ajaxcate( explode( ',' , $pid )[ 0 ] ) ,
                        'lesson'        => $lesson ,
                        'lesson_total'  => $lesson_total ,
                        'ppid'          => explode( ',' , $pid )[ 0 ] ,
                        'select'        => $select ,
                        'ppid_name'     => $ppid_name
                    ] );
                    $cate = '';
                } else if ( empty( $category[ 'type' ] ) ) {
                    $cate = Cate::where( [ 'pid' => $category[ 'id' ] , 'status' => 1 , 'is_deleted' => 1 ] )->hidden( 'create_at,is_deleted,status,sort,pid' )->select()->toArray();
                    foreach ( $cate as &$v ) {
                        if ( $v[ 'type' ] == 'content' ) {
                            $v[ 'content' ] = json_decode( $v[ 'content' ] , true );
                        } elseif ( $v[ 'type' ] == 'lists' ) {
                            $content = CateList::where( [ 'is_deleted' => 1 , 'status' => 1 , 'pid' => $v[ 'id' ] ] )->order( 'sort asc,id asc' )->hidden( 'create_at,is_deleted,status,sort,pid' )->select()->toArray();
                            foreach ( $content as &$vo ) {
                                $vo[ 'content' ] = json_decode( $vo[ 'content' ] , true );
                            }
                            //时间轴数组处理
                            if ( $v[ 'id' ] == 29 ) {
                                $a        = array_column( $content , 'content' );
                                $_content = [];
                                foreach ( $a as $va ) {
                                    $year                = explode( '-' , $va[ 'create_at' ] )[ 0 ];
                                    $month               = explode( '-' , $va[ 'create_at' ] )[ 1 ];
                                    $_content[ $year ][] = [ 'month' => $month , 'txt' => $va[ 'title' ] ];
                                }
                                $content = [];
                                foreach ( $_content as $kc => $vc ) {
                                    $content[] = [ 'year' => $kc , 'event' => $vc ];
                                }
                            }

                            $v[ 'content' ] = $content;
                        }
                    }
                } elseif ( $category[ 'type' ] == 'lists' ) {
                    //核心团队与培训师资需要分页
                    /*if ($category['id']==8 || $category['id']==38){
                        $page = $this->request->get('page',1);
                        $cate = Content::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->page($page,5)->order('sort asc,id asc')->column('content');
                        $total = ceil(Content::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->count() / 5);
                        $this->assign('total',$total);
                    }else{
                        $cate = Content::where(['pid'=>$category['id'],'status'=>1,'is_deleted'=>1])->order('sort asc,id asc')->column('content');
                    }*/

                    $cate = Content::where( [ 'pid' => $category[ 'id' ] , 'status' => 1 , 'is_deleted' => 1 ] )->order( 'sort asc,id asc' )->column( 'content' );
                    foreach ( $cate as &$c ) {
                        $c = json_decode( $c , true );
                    }
                } elseif ( $category[ 'type' ] == 'content' ) {
                    $cate = Content::where( [ 'pid' => $category[ 'id' ] , 'status' => 1 , 'is_deleted' => 1 ] )->value( 'content' );
                    $cate = json_decode( $cate , true );

                }


        $this->assign( [
            'content' => $cate
        ] );
//        pp($cate);die;
    }

    public function ajaxcate( $pid = 0 )
    {
        empty( $pid ) && $pid = $this->request->post( 'pid' );
        $lesson_cate = LessonCate::where( [ 'is_deleted' => 1 , 'status' => 1 ] )->field( 'id,name,pid' )->order( 'sort asc,id asc' )->select()->toArray();
        $lesson_cate = ToolsService::arr2tree( $lesson_cate , 'id' , 'pid' , 'children' );
        $cate        = [];
        foreach ( $lesson_cate as $k => $v ) {
            if ( $v[ 'id' ] == $pid && isset( $v[ 'children' ] ) ) {
                $cate = $v[ 'children' ];
            }
        }
        return $cate;
    }

    public function lesson()
    {
        $id = $this->request->param( 'id' , '' );
        $db = Lesson::where( [ 'id' => $id , 'is_deleted' => 1 , 'status' => 1 ] )->find()->visible( [ 'id' , 'title' , 'desc' , 'leidatu' , 'slider' , 'type' ] )->toArray();
        if ( $db[ 'type' ] == 12 ) {
            $pcate = CategoryModule::get( 12 );
            $this->assign( [
                'pcate' => $pcate ,
                'cur'   => [ 'name' => '课程详情' , 'id' => '' ] ,
            ] );
        } elseif ( $db[ 'type' ] == 39 ) {
            $pcate = CategoryModule::get( 39 );
            $this->assign( [
                'pcate' => CategoryModule::get( $pcate[ 'pid' ] ) ,
                'cur'   => $pcate ,
            ] );
        }

        $db[ 'slider' ]  = explode( '|' , $db[ 'slider' ] );
        $db[ 'desc' ]    = json_decode( $db[ 'desc' ] , true );
        $leidatu_value   = array_column( json_decode( $db[ 'leidatu' ] , true ) , 'value' );
        $db[ 'leidatu' ] = json_decode( $db[ 'leidatu' ] , true );
        foreach ( $db[ 'leidatu' ] as $k => $v ) {
            unset( $db[ 'leidatu' ][ $k ][ 'value' ] );
            $db[ 'leidatu' ][ $k ][ 'max' ] = 10;
        }

        $lesson_content = Db::name( 'lesson_content_list' )->where( [ 'pid' => $id , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'sort asc,id asc' )->select();
        foreach ( $lesson_content as $k => $v ) {
            $lesson_content[ $k ][ 'content' ] = json_decode( $v[ 'content' ] , true );
        }

        $this->assign( [
            'db'             => $db ,
            'leidatu_value'  => $leidatu_value ,
            'lesson_content' => $lesson_content
        ] );
        return view();
    }

    public function school()
    {
        $title = Db::name( 'cate' )->where( 'id' , 35 )->value( 'title' );
        $db    = Db::name( 'cate_list' )->where( [ 'pid' => 35 , 'is_deleted' => 1 , 'status' => 1 ] )->order( 'sort asc,id asc' )->select();
        foreach ( $db as $k => $v ) {
            $db[ $k ][ 'content' ] = json_decode( $v[ 'content' ] , true );
        }
        $this->assign( [
            'db'    => $db ,
            'title' => $title
        ] );
        return view();
    }
}